package com.erp.erp_servers.Resource.imp;

import com.erp.erp_dao.Resource.IResourceDAO;
import com.erp.erp_entitys.Menum.MenumEntity;
import com.erp.erp_entitys.Resource.ResourceEntity;
import com.erp.erp_entitys.Resource.ResourceEntityExample;
import com.erp.erp_entitys.Resource.ResourceTreeEntity;
import com.erp.erp_servers.Resource.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpResourceService
 * @Description 资源实现类
 * @Author songjizhen
 * @Date 2020-06-30 22:46
 * @Version 1.0
 */
@Service
public class ImpResourceService implements IResourceService {
    @Autowired
    IResourceDAO resourceDAO;
    /**
     * 根据主键删除资源
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return resourceDAO.deleteByPrimaryKey(id);
    }

    /**
     * 新增资源
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(ResourceEntity record) {
        if(this.getExitBycode(record.getCode()))record.setCode(System.currentTimeMillis()+"");
        return resourceDAO.insertSelective(record);
    }
private boolean getExitBycode(String code){
    ResourceEntityExample example=new ResourceEntityExample();
    ResourceEntityExample.Criteria criteria = example.createCriteria();
   criteria.andCodeEqualTo(code);
    List<ResourceEntity> resourceEntities = resourceDAO.selectByExample(example);
    return resourceEntities.size()==1;
}
    /**
     * 查询资源
     *
     * @param record
     * @return
     */
    @Override
    public List<ResourceEntity> selectByExample(ResourceEntity record) {
        ResourceEntityExample example=new ResourceEntityExample();
        ResourceEntityExample.Criteria criteria = example.createCriteria();
        if(record.getName() !=null && record.getName() !=""){
            criteria.andNameLike(record.getName());
        }

        if(record.getCode() !=null && record.getCode()!=""){
            criteria.andCodeEqualTo(record.getCode());
        }
        if(record.getPid() !=null && record.getPid() !=""){
            criteria.andPidEqualTo(record.getPid());
        }
        if(record.getUrl() !=null && record.getUrl() !="" && record.getUrl().equals("all")){
            criteria.andUrlIsNotNull();
        }
        List<ResourceEntity> resourceEntities = resourceDAO.selectByExample(example);
        return resourceEntities;
    }

    /**
     * 资源树
     *
     * @return
     */
    @Override
    public List<ResourceTreeEntity> getTree() {
        ResourceEntityExample example=new ResourceEntityExample();
        ResourceEntityExample.Criteria criteria = example.createCriteria();
        List<ResourceEntity> resourceEntities = resourceDAO.selectByExample(example);
        Map<String,ResourceTreeEntity> map=new HashMap<String, ResourceTreeEntity>();
        for(ResourceEntity re:resourceEntities){
            map.put(re.getCode(),this.conver(re));
        }
        List<ResourceTreeEntity> list=new ArrayList<ResourceTreeEntity>();
        ResourceTreeEntity rte=new ResourceTreeEntity();
        rte=this.getList("0",map);
        list.add(rte);
        return list;
    }

    private ResourceTreeEntity conver(ResourceEntity re){
        ResourceTreeEntity rte=new ResourceTreeEntity();
        rte.setCode(re.getCode());
        rte.setId(re.getId());
        rte.setLevel(re.getLevel());
        rte.setName(re.getName());
        rte.setPath(re.getPath());
        rte.setUrl(re.getUrl());
        rte.setPid(re.getPid());
        return rte;
    }
    /**
     * 递归
     * @param code
     * @param map
     * @return
     */
    private ResourceTreeEntity getList(String code, Map<String,ResourceTreeEntity> map){
        ResourceTreeEntity rte=new ResourceTreeEntity();
        rte=map.get(code);
        String pids="";
        List<ResourceTreeEntity> list =new ArrayList<ResourceTreeEntity>();
        for(Map.Entry<String, ResourceTreeEntity> entry : map.entrySet()){
            if(code.equals(entry.getValue().getPid())){
                list.add(this.getList(entry.getValue().getCode(),map));
            }

        }
        rte.setList(list);
        return rte;
       /* String pid=m.getPid();
        MenumEntity me=new MenumEntity();
        if(! map.get(pid).getCode() .equals("0")){
            List<MenumEntity> list=new ArrayList<MenumEntity>();
            list.add(m);
            map.get(pid).setList(list);
            me= this.getParentTree(map.get(pid), map);

        }else{
            me=map.get(pid);
            List<MenumEntity> list=new ArrayList<MenumEntity>();
            list.add(m);
            me.setList(list);
        }
        return me;
        return null;*/
}
    /**
     * 根据code查询符合条件的资源
     * @param list
     * @return
     */
    @Override
    public List<ResourceEntity> selectByExample( List<String> list) {
        ResourceEntityExample example=new ResourceEntityExample();
        ResourceEntityExample.Criteria criteria = example.createCriteria();
        if(list !=null && list.size()>0){
            criteria.andCodeIn(list);
        }

        example.setOrderByClause("path");
        List<ResourceEntity> resourceEntities = resourceDAO.selectByExample(example);
        return resourceEntities;
    }

    /**
     * 更新资源
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ResourceEntity record) {
        return resourceDAO.updateByPrimaryKeySelective(record);
    }
}
